package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.PbBean.PbUserMessage;
import com.jwdf.uda.databinding.ItemMessageBinding;
import com.jwdf.uda.ui.activity.GuideDetailActivity;
import com.jwdf.uda.ui.activity.H5Activity;
import com.jwdf.uda.ui.activity.QuizDetailActivity;
import com.jwdf.uda.ui.activity.ReplyDetaiActivtiy;
import com.jwdf.uda.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 * 作者：huangqiuxin on 2016/5/17 15:56
 * 邮箱：648859026@qq.com
 */
public class MessageAdapter extends AbsAdapter<PbUserMessage> {
    private UserMessageClickListener mListener;

    public MessageAdapter(Context context) {
        super(context);
    }

    public List<Integer> getUnReadMessageList() {
        List<Integer> unReadMsgs = new ArrayList<>();

        List<PbUserMessage> msgs = new ArrayList<>(mData);
        for (PbUserMessage message : msgs) {
            if (!message.getIsRead()) {
                unReadMsgs.add(message.getId());
            }
        }
        return unReadMsgs;
    }

    public void setUserMessageClickListener(UserMessageClickListener listener) {
        this.mListener = listener;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new MessageViewHolder(mInflater.inflate(R.layout.item_message, parent, false));
    }

    public class MessageViewHolder extends AbsViewHolder<PbUserMessage> {
        private ItemMessageBinding mBinding;

        public MessageViewHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(final PbUserMessage item) {
            PbBean.PbSignUserMessageType type = item.getType();
            String msgType = convertView.getContext().getString(R.string.sys_notice);
            switch (type) {
                case USER_MESSAGE_TYPE_SYSTEM:
                    msgType = convertView.getContext().getString(R.string.sys_notice);
                    break;
                case USER_MESSAGE_TYPE_COMMENT:
                    msgType = convertView.getContext().getString(R.string.comment_notice);
                    break;
                case USER_MESSAGE_TYPE_LIKE:
                    msgType = convertView.getContext().getString(R.string.like_notice);
                    break;
                case USER_MESSAGE_TYPE_FLOW:
                    msgType = convertView.getContext().getString(R.string.flow_notice);
                    break;
                case USER_MESSAGE_TYPE_VOTE:
                    msgType = convertView.getContext().getString(R.string.vote_notice);
                    break;
                case USER_MESSAGE_TYPE_QUESTION_ANSWER:
                    msgType = convertView.getContext().getString(R.string.question_answer_notice);
                    break;
                case USER_MESSAGE_TYPE_QUESTION_INVITE:
                    msgType = convertView.getContext().getString(R.string.question_answer_invite);
                    break;
            }
            mBinding.tvMsgType.setText(msgType);
            mBinding.tvCreattime.setText(DateUtils.getSimpleDateTime(1000l * item.getCreateTime()));
            mBinding.tvContent.setText(item.getContent());
            mBinding.itemRoot.setSelected(item.getIsRead() ? true : false);
            mBinding.tvContent.setTextColor(item.getIsRead() ? Color.parseColor("#b2b2b2") : Color.parseColor("#333333"));
            mBinding.ivRedDot.setVisibility(item.getIsRead() ? View.GONE : View.VISIBLE);
            mBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener(v.getContext(), item);
                }
            });
        }
    }

    private void onItemClickListener(Context context, PbBean.PbUserMessage item) {
        PbBean.PbSignNavType navType = item.getNav().getType();
        int detailId = item.getNav().getId();
        boolean isRead = item.getIsRead();
        switch (navType) {
            case LINK_TYPE_GUIDE:
                if (!isRead) {
                    GuideDetailActivity.startActivity(context, detailId, item.getId());
                } else {
                    GuideDetailActivity.startActivity(context, detailId);
                }
                break;
            case LINK_TYPE_QUESTION:
                if (!isRead) {
                    QuizDetailActivity.start(context, detailId, item.getId());
                } else {
                    QuizDetailActivity.start(context, detailId);
                }
                break;
            case LINK_TYPE_QUESTION_ANSWER:
                if (!isRead) {
                    ReplyDetaiActivtiy.start(context, detailId, item.getId());
                } else {
                    ReplyDetaiActivtiy.start(context, detailId);
                }
                break;
            case LINK_TYPE_USER_MESSAGE:
                if (mListener != null) mListener.onClick(context, item.getId());
                break;
            case LINK_TYPE_WEB:
            case LINK_TYPE_OUTSIDE_WEB:
                H5Activity.start(context, "http://www.baidu.com/");
                break;
        }
    }

    public interface UserMessageClickListener {
        void onClick(Context context, int msgId);
    }

}
